package com.gdpu.project.action.achievementManager;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.gdpu.page.vo.PageView;
import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.service.ChengGuoService;
import com.gdpu.project.service.TeacherService;
import com.gdpu.project.util.DateUtil;
import com.gdpu.project.vo.CgThesis;
import com.gdpu.project.vo.Teacher;
import com.opensymphony.xwork2.ActionSupport;

public class DissertationAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private int id;
	private String thesisId;
	private String teacherName;
	private String thesisTitle;
	private String kanWu;
	private String publishDate;
	private String suoyin;
	private String grade;
	private float keYanFen;
	private float reword;
	private String comment;
	private String thesisType;
	private String thesisEnTitle;
	private String juanHao;
	private String schoolSign;
	private String publishRange;
	private String qiHao;
	private String pageRange;
	private String ziShu;
	private String projectOrigin;
	private String teacherId;
	private String xueKe;
	private String layout;
	private String yiWen;
	private String thesisZY;
	private String layCost;
	private HttpServletRequest request;
	private HttpSession session;
	private DateUtil dateUtil = new DateUtil();
	private TeacherService teacherService;
	private ChengGuoService chengguoService;
 
    private PageView<CgThesis> pageView;
    private int currentpage=1;
	

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public PageView<CgThesis> getPageView() {
		return pageView;
	}

	public void setPageView(PageView<CgThesis> pageView) {
		this.pageView = pageView;
	}

	public String getSuoyin() {
		return suoyin;
	}

	public void setSuoyin(String suoyin) {
		this.suoyin = suoyin;
	}

	public void setChengguoService(ChengGuoService chengguoService) {
		this.chengguoService = chengguoService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getThesisId() {
		return thesisId;
	}

	public void setThesisId(String thesisId) {
		this.thesisId = thesisId;
	}

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public String getThesisType() {
		return thesisType;
	}

	public void setThesisType(String thesisType) {
		this.thesisType = thesisType;
	}

	public String getThesisTitle() {
		return thesisTitle;
	}

	public void setThesisTitle(String thesisTitle) {
		this.thesisTitle = thesisTitle;
	}

	public String getThesisEnTitle() {
		return thesisEnTitle;
	}

	public void setThesisEnTitle(String thesisEnTitle) {
		this.thesisEnTitle = thesisEnTitle;
	}

	public String getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(String publishDate) {
		this.publishDate = publishDate;
	}

	public String getKanWu() {
		return kanWu;
	}

	public void setKanWu(String kanWu) {
		this.kanWu = kanWu;
	}

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public String getJuanHao() {
		return juanHao;
	}

	public void setJuanHao(String juanHao) {
		this.juanHao = juanHao;
	}

	public String getSchoolSign() {
		return schoolSign;
	}

	public void setSchoolSign(String schoolSign) {
		this.schoolSign = schoolSign;
	}

	public String getPublishRange() {
		return publishRange;
	}

	public void setPublishRange(String publishRange) {
		this.publishRange = publishRange;
	}

	public String getQiHao() {
		return qiHao;
	}

	public void setQiHao(String qiHao) {
		this.qiHao = qiHao;
	}

	public String getPageRange() {
		return pageRange;
	}

	public void setPageRange(String pageRange) {
		this.pageRange = pageRange;
	}

	public String getZiShu() {
		return ziShu;
	}

	public void setZiShu(String ziShu) {
		this.ziShu = ziShu;
	}

	public String getProjectOrigin() {
		return projectOrigin;
	}

	public void setProjectOrigin(String projectOrigin) {
		this.projectOrigin = projectOrigin;
	}

	public String getXueKe() {
		return xueKe;
	}

	public void setXueKe(String xueKe) {
		this.xueKe = xueKe;
	}

	public String getLayout() {
		return layout;
	}

	public void setLayout(String layout) {
		this.layout = layout;
	}

	public String getYiWen() {
		return yiWen;
	}

	public void setYiWen(String yiWen) {
		this.yiWen = yiWen;
	}

	public float getKeYanFen() {
		return keYanFen;
	}

	public void setKeYanFen(float keYanFen) {
		this.keYanFen = keYanFen;
	}

	public String getThesisZY() {
		return thesisZY;
	}

	public void setThesisZY(String thesisZY) {
		this.thesisZY = thesisZY;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public void setLayCost(String layCost) {
		this.layCost = layCost;
	}

	public String getLayCost() {
		return layCost;
	}

	public float getReword() {
		return reword;
	}

	public void setReword(float reword) {
		this.reword = reword;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	// 增加论文
	public String addDissertation() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		CgThesis cgThesis = new CgThesis();
		cgThesis.setTeacherName(teacherName);
		cgThesis.setThesisTitle(thesisTitle);
		cgThesis.setKanWu(kanWu);
		cgThesis.setSuoyin(suoyin);
		cgThesis.setGrade(grade);
		cgThesis.setPublishDate(DateUtil.stringToDate(publishDate));
		cgThesis.setReword(reword);
		cgThesis.setComment(comment);
		cgThesis.setJuanHao(getJuanHao());
		cgThesis.setLayout(getLayout());
		cgThesis.setPageRange(getPageRange());
		cgThesis.setProjectOrigin(getProjectOrigin());
		cgThesis.setPublishRange(getPublishRange());
		cgThesis.setQiHao(getQiHao());
		cgThesis.setXueKe(getXueKe());
		cgThesis.setYiWen(getYiWen());
		cgThesis.setSchoolSign(getSchoolSign());
		cgThesis.setTeacherId(getTeacherId());
		cgThesis.setThesisEnTitle(getThesisEnTitle());
		cgThesis.setThesisId(getThesisId());
		cgThesis.setThesisType(getThesisType());
		cgThesis.setThesisZy(getThesisZY());
		cgThesis.setZiShu(getZiShu());
		cgThesis.setLayout(getLayout());
		cgThesis.setLayoutCost(this.getLayCost());
		//set对应的教师编号
		List<Teacher> teacherList=teacherService.findByName(teacherName);
		if(teacherList.size()>0){
			cgThesis.setTeacherId(teacherList.get(0).getTeacherId());
		}
		chengguoService.addCgThesis(cgThesis);
		
		//角色判断，管理员返回所有，教职工返回相应的
		List<CgThesis> cgThesislist=this.findAllCgThesisDepRole();
		
		
		request.setAttribute("cgThesislist", cgThesislist);
		return SUCCESS;
	}

	

	// 查找所有论文
	public String findAllDissertation() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		List<CgThesis> cgThesislist = findAllCgThesisDepRole();//角色判断，管理员返回所有，教职工返回相应的
		request.setAttribute("cgThesislist", cgThesislist);
		return SUCCESS;
	}
	
	// 根据id查找论文
	public String findDissertationById() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		chengguoService.findCgThesis(getId());
		CgThesis cgThesis = chengguoService.findCgThesis(this.getId());
//		System.out.println(cgThesis.getProjectOrigin());
		
		request.setAttribute("cgThesis", cgThesis);
		return SUCCESS;
	}

	// 根据id删除论文
	public String delDissertationById() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		chengguoService.deleteCgThesisById(getId());
		
		//角色判断，管理员返回所有，教职工返回相应的
		List<CgThesis> cgThesislist = findAllCgThesisDepRole();
		
		request.setAttribute("cgThesislist", cgThesislist);
		return SUCCESS;
	}

	

	// 更新论文
	public String updateDissertation() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		CgThesis cgThesis = chengguoService.findCgThesis(getId());
		cgThesis.setTeacherName(teacherName);
		cgThesis.setThesisTitle(thesisTitle);
		cgThesis.setGrade(grade);
		cgThesis.setSuoyin(suoyin);
		cgThesis.setKanWu(kanWu);
		cgThesis.setPublishDate(DateUtil.stringToDate(publishDate));
		cgThesis.setKeYanFen(keYanFen);
		cgThesis.setReword(reword);
		cgThesis.setComment(comment);
		
		cgThesis.setJuanHao(getJuanHao());
		cgThesis.setLayout(getLayout());
		cgThesis.setPageRange(getPageRange());
		cgThesis.setProjectOrigin(getProjectOrigin());
		cgThesis.setPublishRange(getPublishRange());
		cgThesis.setQiHao(getQiHao());
		cgThesis.setXueKe(getXueKe());
		cgThesis.setYiWen(getYiWen());
		cgThesis.setSchoolSign(this.getSchoolSign());
		cgThesis.setThesisEnTitle(getThesisEnTitle());
		cgThesis.setThesisId(getThesisId());
		cgThesis.setThesisType(getThesisType());
		cgThesis.setThesisZy(getThesisZY());
		cgThesis.setZiShu(getZiShu());
		cgThesis.setLayoutCost(this.getLayCost());
		//set对应的教师编号
		List<Teacher> teacherList=teacherService.findByName(teacherName);
		if(teacherList.size()>0){
			cgThesis.setTeacherId(teacherList.get(0).getTeacherId());
		}
		chengguoService.updateCgThesis(cgThesis);
//		if(flag==true){
//			request.setAttribute("message", "<script>alert('修改成功！')</script>");
//		}		
		List<CgThesis> cgThesislist=this.findAllCgThesisDepRole();
		
		request.setAttribute("cgThesislist", cgThesislist);
		return SUCCESS;
	}
	
	/**角色判断，管理员返回所有，教职工返回相应的
	 * @return List<CgThesis>
	 */
	private List<CgThesis> findAllCgThesisDepRole() {
		pageView=new PageView<CgThesis>(10,currentpage);//存取页码等信息,每页10条记录,当前页由页面传入
		Teacher teacher=(Teacher)session.getAttribute("teacher");
		 QueryResult<CgThesis>  queryResult=null;
		 if(teacher!=null){
			if(teacher.getRole()==1){
				queryResult = chengguoService.findAllCgThesis(pageView.getFirstResult(), pageView.getMaxresult());		
			}else{
				queryResult = chengguoService.findAllCgThesisByTeacherId(teacher.getTeacherId(),pageView.getFirstResult(), pageView.getMaxresult());
			}
			pageView.setQueryResult(queryResult);
			request.setAttribute("pageView",pageView);
			return queryResult.getResultlist();
		 }
		 return null;
	}

	
}
